
$(function () {
    ShowCurrentMenu();
    HelpCenter();

    $("#ulMainMenu li").hover(function () {

        $(this).siblings().attr("class", "");
        $(this).attr("class", "now");

    }, function () {
        $(this).attr("class", "");
    });

    $("#ulMainMenu").hover(function () {

    }, function () {
        ShowCurrentMenu();
    });


    //return;

    $("#txtSearchKey").mousemove(function () {
        $("#topSearchSlideBox").show();
    });

    $("#txtSearchKey").mouseleave(function () {
        $("#topSearchSlideBox").hide();
    });

    $("#topSearchSlideBox").mousemove(function () {
        $("#topSearchSlideBox").show();
    });

    $("#topSearchSlideBox").mouseleave(function () {
        $("#topSearchSlideBox").hide();
    });

    $("#txtSearchKey").keyup(GetSearchKeyByKey);

    GetDefaultSearch();

})

function GetSpecialUrl(url)//  cunchu/215/247061.html
{
    var keyName = url.split("/")[0];

    var hostName = window.location.host;

    switch (keyName) {


        //case "zhenwei":
        //    return "http://" + hostName + "/zhenwei/";

        //case "cunchu":
        //    return "http://" + hostName + "/cunchu/";

        //case "pingce":
        //    return "http://" + hostName + "/pingce/";

        case "jiedu":
            return "http://" + hostName + "/jiedu/";

        default:
            return "";

    }

}

function GetMenuID() {



    var relativeUrl = GetUrlRelativePath();                                 //  wenhua/207/240946.html
    var specialUrl = GetSpecialUrl(relativeUrl);                            //  特殊地址

    if (specialUrl != "") {
        return specialUrl;
    }

    var currentUrl = GetUrlMainPath();                                      //  http://www.zyctd.net/zixun/201-2.html   8-0-0-1.html  207/240946.html   xq1.html


    if (currentUrl.substring(currentUrl.length - 5) == ".html") {
        if (currentUrl.indexOf("-") != -1) {                                //  带-的
            currentUrl = currentUrl.split('-')[0] + "/";
        } else {
            if (relativeUrl.indexOf("/") != -1) {                           //  wenhua/207/240946.html  jiage/xq1.html
                currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/') + 1);
            } else {                                                        //  jh32.html 
                currentUrl = "http://yaocai" + ZYCLib.Domain + "/";
            }
        }
    }

    return currentUrl;

}

function ShowCurrentMenu() {

    var currentUrl = GetMenuID();



    $("#ulMainMenu li").each(function (index, obj) {

        if (currentUrl == $(obj).attr("menuid")) {
            $(obj).attr("class", "now");

            return;
        }

    })


    $("#ulMainMenu div a").each(function (index, obj) {

        if (currentUrl == $(obj).attr("menuid")) {

            $(obj).css({ "color": "#333", "font-weight": "bold" }).siblings().css("color", "#666");
            $(obj).parent().parent().attr("class", "now");
            return;
        }

    })





}

function btnSearchKeyClick() {
    var keyword = $.trim($("#txtSearchKey").val());

    if (keyword == "") {
        return;
    }

    if (!validateUtility.isValidBaseString(keyword)) {
        ZYCLib.UI.MessageBox.Alert("输入的关键字包含非法字符!");
        return;
    }

    var arrKey = [];
    var cooKeyWord = GetCookie("ZyctdKeyWord");

    if (cooKeyWord == null) cooKeyWord = "";

    cooKeyWord = keyword + "|" + cooKeyWord;
    arrKey = UniqueKey(cooKeyWord.split("|"));

    var newCooKey = "";

    for (var i = 0; i < arrKey.length; i++) {
        newCooKey += arrKey[i] + "|";
    }

    var domain = GetCurrentDomain();
    SetCookieForDay("ZyctdKeyWord", newCooKey, 1000,domain);

    var url = "http://www" + ZYCLib.Domain + "/Search/Index?keyword=" + encodeURIComponent(keyword);

    window.location.href = url;

    //window.open("http://www" + ZYCLib.Domain + "/Search/Index?keyword=" + encodeURIComponent(keyword) + "");



}

function HelpCenter() {
    var relativeUrl = GetUrlRelativePath();     //  Help/job
    var menuName = relativeUrl.split("/")[0];

    if (menuName == "Help") {
        var cataid = relativeUrl.split("/")[1];

        var currentCata = $("#ulHelpCata li[cataid=" + cataid + "]");
        $(currentCata).siblings().attr("class", "");
        $(currentCata).attr("class", "active");

    }
}

function GetDefaultSearch() {

    var sendData = {};
    sendData.PageIndex = 1;
    sendData.PageSize = 10;
    sendData.Keyword = "";
    sendData.SearchType = 8;

    ZYCLib.Web.JsonAjax.PostData("common/SearchService/SearchDataSearchByWeb", sendData, function (ret) {

        var htmlStr = "";

        for (var i = 0; i < ret.SearchPageData.Datas.length; i++) {
            var codex = ret.SearchPageData.Datas[i];

            htmlStr += "<a href='javascript:void(0)' onclick='KeyClick(this)' >" + codex.Keyword + "</a>";
        }

        $("#hotKeywordsBox").html(htmlStr);

    }, function () {
    })

    var arrKey = [];
    var cooKeyWord = GetCookie("ZyctdKeyWord");

    if (cooKeyWord == null) cooKeyWord = "";

    arrKey = cooKeyWord.split("|");

    var htmlStr = "";

    for (var i = 0; i < arrKey.length & i < 10; i++) {
        var codex = arrKey[i];

        if (codex == null || codex == "") {
            continue;
        }

        htmlStr += "<a href='javascript:void(0)' onclick='KeyClick(this)' >" + codex + "</a>";
    }


    $("#keyHistoryBox").html(htmlStr);


}

function GetSearchKeyByKey() {

    var keyword = $.trim($("#txtSearchKey").val());

    if (keyword == "") {
        $("#topSearchSlideDefault").show();
        $("#topSearchAutoList").hide();
    } else {
        $("#topSearchSlideDefault").hide();
        $("#topSearchAutoList").show();
    }

    var sendData = {
    };
    sendData.PageIndex = 1;
    sendData.PageSize = 10;
    sendData.Keyword = keyword;
    sendData.SearchType = 8;

    ZYCLib.Web.JsonAjax.PostData("common/SearchService/SearchDataSearchByWeb", sendData, function (ret) {

        var htmlStr = "";

        for (var i = 0; i < ret.SearchPageData.Datas.length; i++) {
            var codex = ret.SearchPageData.Datas[i];

            htmlStr += "<a href='javascript:void(0)' onclick='KeyClick(this)' >" + codex.Keyword + "</a>";
        }

        $("#topSearchAutoList").html(htmlStr);



    }, function () {
    })

}

function KeyClick(obj) {
    var keyword = $(obj).html();

    var url = "http://www" + ZYCLib.Domain + "/Search/Index?keyword=" + encodeURIComponent(keyword);

    window.location.href = url;
}

function UniqueKey(arr) {

    var result = [], have = {};

    for (var i = 0; i < 10; i++) {
        var temp = arr[i];

        if (temp == null || temp == "") {
            continue;
        }

        if (!have[temp]) {
            result.push(temp);
            have[temp] = true;
        }
    }

    return result;
}